using IceZero.Business.Models;
using IceZero.Controllers;
using IceZero.Web.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;

namespace IcsZero.Client.WebMvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews(options=> {
                options.Filters.Add<IceZeroActionFilter>();
                options.Filters.Add<IceZeroAuthorizationFilter>();
            });
            services.AddSwaggerGen(options =>
            {
                var license = new OpenApiLicense()
                {
                    Name = "GPL V3",
                    Url = new Uri("https://www.example.com/gpl_v3.html")
                };
                var contact = new OpenApiContact()
                {
                    Name = "DHclly",
                    Email = "335121817@qq.com",
                    Url = new Uri("https://www.example.com/contact_me.html")
                };
                var description = "this project use for test WebApi Anything";
                options.SwaggerDoc("version_1_0", new OpenApiInfo
                {

                    Title = "IceZero API",
                    Version = "v1.0",
                    License = license,
                    Contact = contact,
                    Description = description
                });

                options.SwaggerDoc("version_test", new OpenApiInfo
                {

                    Title = "IceZero Test API",
                    Version = "test",
                    License = license,
                    Contact = contact,
                    Description = description
                });

                //文档包含策略
                options.DocInclusionPredicate((docName, options) =>
                {
                    if(docName== "version_test")
                    {
                        return options.GroupName == docName;
                    }
                    return true;
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                if (File.Exists(xmlPath))
                {
                    options.IncludeXmlComments(xmlPath);
                }

                xmlFile = $"{typeof(ActionStatisticController).Assembly.GetName().Name}.xml";
                xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                if (File.Exists(xmlPath))
                {
                    options.IncludeXmlComments(xmlPath);
                }

                xmlFile = $"{typeof(IceZeroBusinessModelsProfile).Assembly.GetName().Name}.xml";
                xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                if (File.Exists(xmlPath))
                {
                    options.IncludeXmlComments(xmlPath);
                }
                //所有参数变量用camelCase
                options.DescribeAllParametersInCamelCase();
            });
            services.AddIceZeroUserDomainWeb();
            services.AddIceZeroWeb();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapAreaControllerRoute(
                    name: "areaRoute",
                    areaName: "areas",
                    pattern: "{areas=Server}/{controller=ServerInfo}/{action=Index}/{id?}");
                //endpoints.MapSwagger();
            });

            app.UseSwagger(options =>
            {
                options.RouteTemplate = "swagger/{documentName}/swagger.json";
            });

            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("version_1_0/swagger.json", "IceZero API Version 1.0");
                options.SwaggerEndpoint("version_test/swagger.json", "IceZero Test API Version 1.0");
            });
        }
    }
}
